
// 点击登录按钮出现登录界面
function getLogin() {
    $('.loginBtn').on('click', function () {
        if($('.loginBtn span:nth-child(2)').text() == '登录/注册')
        $('#login>div').css('display', 'block')
        $('#login .guanbi').on('click', function () {
            $('#login>div').css('display', 'none')
        })
    })
}
// 切换注册与登录界面
function changeLogin(){
    $('#login form>div').on('click','div',function(e){
        let target = e.target || e.srcElement
        if($(target).text() == '登录'){
            $('#login form>div>div').removeClass('active')
            $(target).addClass('active')
            $('#login form input[value="注册"]').val('登录')
        }
        if($(target).text() == '注册'){
            $('#login form>div>div').removeClass('active')
            $(target).addClass('active')
            $('#login form input[value="登录"]').val('注册')
        }
    })
}
// 输入账号密码进行注册
function register(){
    // 点击注册按钮后获取input中值连接数据库进行注册
    $('#login form').on('click','input[value="注册"]',async function(){
        // 获取input--value值
        let username = $('input[name="username"]').val()
        let password = $('input[name="password"]').val()
        let data = await myPromise({
            method: 'post',
            url: 'http://api.yuguoxy.com/api/member/add',
            data:{
                username,
                password
            }
        })
        if (data.resultCode == 1) {
            let registerStr = data.resultCode == 1 ? data.resultInfo : []
            console.log(registerStr,username,password)
            let newuser = {
                username,
                password
            }
            console.log(newuser)
            let userLocalStr = localStorage.getItem('user')
            let userlocal = JSON.parse(userLocalStr) || []
            userlocal.push(newuser)
            localStorage.setItem('user',JSON.stringify(userlocal))
            alert('会员信息注册成功，请登录！')
        }
    })
}

// 输入账号及密码从localStorage会员信息
function login() {
    // 点击提交按钮之后获取input值与数据库确认信息
    $('#login form').on('submit', async function (e) {
        e = e || window.event //事件对象
        e.preventDefault() //阻止默认行为
        // 调用登录接口 
        let username = $('input[name="username"]').val()
        let password = $('input[name="password"]').val()
        let data = await myPromise({
            method: 'post',
            url: 'http://api.yuguoxy.com/api/member/login',
            data:{
                username,
                password
            }
        })
        if (data.resultCode == 1) {
            let user = data.resultCode == 1 ? data.resultInfo : []
            console.log(user)
            // let userLocalStr = localStorage.getItem('userInfo')
            // let userlocal = JSON.parse(userLocalStr) || []
            // userlocal.push(user)
            localStorage.setItem('userInfo',JSON.stringify(user))
            alert('登录成功')
            $('#login>div').css('display', 'none')
            $('.loginBtn').html(`<div><img src="${user.headerimg}" alt=""></div>
            <span>${user.username}</span><span class="logout">退出登录</span>`)

        }
    })
}
// 从localStorage中获取用户信息并展示到页面
function showUser(){
    let userStr = localStorage.getItem('userInfo')
    let user = JSON.parse(userStr)
    $('.loginBtn').html(`<div><img src="${user.headerimg}" alt=""></div>
    <span>${user.username}</span><span class="logout">退出登录</span>`)
    logout()
}


// 点击退出登录按钮退出登录
function logout(){
    $('.logout').on('click',function(){
        localStorage.removeItem('userInfo')
        $('.loginBtn').html(`<span class="iconfont icon-denglu"></span>
        <span>登录/注册</span>`)
    })
}

getLogin()
changeLogin()
register()
login()
showUser()